﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SEO.Business;

/// <summary>
/// 文章收录统计
/// </summary>
public partial class UserArticleReportList : AdminPage
{
    /// <summary>
    /// 用户ID
    /// </summary>
    protected int UserID
    {
        get { return ViewState["UserID"] == null ? 0 : Convert.ToInt32(ViewState["UserID"]); }
        set { ViewState["UserID"] = value; }
    }

    /// <summary>
    /// 开始时间
    /// </summary>
    protected string BeginDateTime
    {
        get { return ViewState["BeginDateTime"] == null ? "" : ViewState["BeginDateTime"].ToString(); }
        set { ViewState["BeginDateTime"] = value; }
    }

    /// <summary>
    /// 结束时间
    /// </summary>
    protected string EndDateTime
    {
        get { return ViewState["EndDateTime"] == null ? "" : ViewState["EndDateTime"].ToString(); }
        set { ViewState["EndDateTime"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindUser();
            InitData();
        }
    }

    /// <summary>
    /// 权限控制
    /// </summary>
    /// <returns></returns>
    public override long GetNeededFeatures()
    {
        return AdminFEATURE.UserArticleReportList;
    }

    /// <summary>
    /// 绑定用户列表
    /// </summary>
    protected void BindUser()
    {
        DataTable dtUser = new BisAdminInfo().GetAllUserList();
        ddlUser.Items.Clear();
        ddlUser.Items.Add(new ListItem("全部", "0"));
        foreach (DataRow drUser in dtUser.Rows)
        {
            ddlUser.Items.Add(new ListItem(drUser["Name"].ToString(), drUser["ID"].ToString()));
        }
        dtUser.Dispose();
        txtBeginDateTime.Text = DateTime.Now.ToString("yyyy-MM-01");
        BeginDateTime = txtBeginDateTime.Text.Trim();
        txtEndDateTime.Text = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
        EndDateTime = txtEndDateTime.Text.Trim();
    }

    /// <summary>
    /// 初始化列表
    /// </summary>
    protected void InitData()
    {
        int nOurWebCount = 0;
        int nOurWebUserCount = 0;
        int nNewsAllCount = 0;
        int nNewsAllSEOCount = 0;
        int nNewsCount1 = 0;
        int nNewsCount2 = 0;
        int nLinkAllCount = 0;
        int nLinkAllSEOCount = 0;
        int nLinkCount1 = 0;
        int nLinkCount2 = 0;
        DateTime dBeginTime = Convert.ToDateTime(BeginDateTime);
        DateTime dEndTime = Convert.ToDateTime(EndDateTime);
        DataTable dt = new BisUserArticleReport().GetList(UserID, dBeginTime, dEndTime);
        DataTable dtNow = dt.Clone();
        int nDay = (dEndTime - dBeginTime).Days;
        if (nDay >= 0)
        {
            for (int i = 0; i <= nDay; i++)
            {
                DataRow drNow = dtNow.NewRow();
                bool bTag = false;
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["CreateTime"].ToString() == dBeginTime.AddDays(i).ToString("yyyy-MM-dd"))
                    {
                        drNow["CreateTime"] = dr["CreateTime"].ToString();
                        drNow["OurWebCount"] = Convert.ToInt32(dr["OurWebCount"]);
                        drNow["OurWebUserCount"] = Convert.ToInt32(dr["OurWebUserCount"]);
                        drNow["NewsAllCount"] = Convert.ToInt32(dr["NewsAllCount"]);
                        drNow["NewsAllSEOCount"] = Convert.ToInt32(dr["NewsAllSEOCount"]);
                        drNow["NewsCount1"] = Convert.ToInt32(dr["NewsCount1"]);
                        drNow["NewsCount2"] = Convert.ToInt32(dr["NewsCount2"]);
                        drNow["LinkAllCount"] = Convert.ToInt32(dr["LinkAllCount"]);
                        drNow["LinkAllSEOCount"] = Convert.ToInt32(dr["LinkAllSEOCount"]);
                        drNow["LinkCount1"] = Convert.ToInt32(dr["LinkCount1"]);
                        drNow["LinkCount2"] = Convert.ToInt32(dr["LinkCount2"]);

                        nOurWebCount += Convert.ToInt32(dr["OurWebCount"]);
                        nOurWebUserCount += Convert.ToInt32(dr["OurWebUserCount"]);
                        nNewsAllCount += Convert.ToInt32(dr["NewsAllCount"]);
                        nNewsAllSEOCount += Convert.ToInt32(dr["NewsAllSEOCount"]);
                        nNewsCount1 += Convert.ToInt32(dr["NewsCount1"]);
                        nNewsCount2 += Convert.ToInt32(dr["NewsCount2"]);
                        nLinkAllCount += Convert.ToInt32(dr["LinkAllCount"]);
                        nLinkAllSEOCount += Convert.ToInt32(dr["LinkAllSEOCount"]);
                        nLinkCount1 += Convert.ToInt32(dr["LinkCount1"]);
                        nLinkCount2 += Convert.ToInt32(dr["LinkCount2"]);
                        bTag = true;
                        break;
                    }
                }
                if (!bTag)
                {
                    drNow["CreateTime"] = dBeginTime.AddDays(i).ToString("yyyy-MM-dd");
                    drNow["OurWebCount"] = 0;
                    drNow["OurWebUserCount"] = 0;
                    drNow["NewsAllCount"] = 0;
                    drNow["NewsAllSEOCount"] = 0;
                    drNow["NewsCount1"] = 0;
                    drNow["NewsCount2"] = 0;
                    drNow["LinkAllCount"] = 0;
                    drNow["LinkAllSEOCount"] = 0;
                    drNow["LinkCount1"] = 0;
                    drNow["LinkCount2"] = 0;
                }
                dtNow.Rows.Add(drNow);
            }
        }
        rptReport.DataSource = dtNow;
        rptReport.DataBind();
        dt.Dispose();
        dtNow.Dispose();

        ltlOurWebCount.Text = nOurWebCount.ToString();
        ltlOurWebUserCount.Text = nOurWebUserCount.ToString();
        ltlNewsAllCount.Text = nNewsAllCount.ToString();
        ltlNewsAllSEOCount.Text = nNewsAllSEOCount.ToString();
        ltlNewsCount1.Text = nNewsCount1.ToString();
        ltlNewsCount2.Text = nNewsCount2.ToString();
        ltlLinkAllCount.Text = nLinkAllCount.ToString();
        ltlLinkAllSEOCount.Text = nLinkAllSEOCount.ToString();
        ltlLinkCount1.Text = nLinkCount1.ToString();
        ltlLinkCount2.Text = nLinkCount2.ToString();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        UserID = Convert.ToInt32(ddlUser.SelectedValue);
        BeginDateTime = txtBeginDateTime.Text.Trim();
        EndDateTime = txtEndDateTime.Text.Trim();
        InitData();
    }

    /// <summary>
    /// 生成Excel
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "window.open('CreateExcel.aspx?type=userarticlereport&begintime=" + BeginDateTime + "&endtime="+EndDateTime+"&userid=" + UserID + "');", true);
    }
}